package ui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseTrackListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.MouseMoveListener;
//2021.6.20 14:31修改优化了类的结构设计，接口并未修改
//2021.6.21 修改:优化了类的结构设计，圆点也会跟着一起动，接口并未修改
//2021.6.21再次优化
public abstract class AbstractProgressBar extends Composite implements MouseMoveListener, MouseListener, MouseTrackListener, PaintListener{

	public volatile double progress;
	protected Label progressPoint;
	protected boolean isDrag;
	protected int currentMouseX;
	private AbstractProgressBar thisOBJ;
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public AbstractProgressBar(Composite parent, int style) {
		super(parent, style);
		thisOBJ = this;
		progressPoint = new Label(this, SWT.NONE);
		progressPoint.setSize(11, 11);
		progressPoint.setVisible(false);
		progressPoint.setImage(new Image(getDisplay(), "src/image/progressPoint.png"));
		progressPoint.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				e.x +=  progressPoint.getLocation().x;
				thisOBJ.mouseUp(e);
			}
			@Override
			public void mouseDown(MouseEvent e) {
				e.x +=  progressPoint.getLocation().x;
				thisOBJ.mouseDown(e);
			}
		});
		progressPoint.addMouseMoveListener(new MouseMoveListener() {
			
			@Override
			public void mouseMove(MouseEvent e) {
				e.x +=  progressPoint.getLocation().x;
				thisOBJ.mouseMove(e);
			}
		});
		addMouseMoveListener(this);
		addPaintListener(this);
		addMouseListener(this);
		addMouseTrackListener(this);
	}

	@Override
	protected void checkSubclass() {}

	@Override
	public void mouseEnter(MouseEvent e) {
		if (!isDrag) {
			progressPoint.setVisible(true);
		}
	}

	@Override
	public void mouseExit(MouseEvent e) {
		if (e.x < getSize().x && e.y < getSize().y) {
			return;
		}
		if (!isDrag) {
			redraw();
			progressPoint.setVisible(false);
		}
	}

	@Override
	public void mouseHover(MouseEvent e) {}

	@Override
	public void mouseDoubleClick(MouseEvent e) {}

	@Override
	public void mouseDown(MouseEvent e) {
		currentMouseX = e.x;
		progressPoint.setLocation(e.x, 0);
		progress = e.x * 1.0 / getBounds().width;
		progressPoint.setLocation((int)(progress * getBounds().width), 0);
		GC gc = new GC(this);
		gc.setLineWidth(5);
		gc.setForeground(new Color(getDisplay(), 240, 240, 240 ));
		gc.drawLine(0, 4, getBounds().width, 4);
		gc.setForeground(new Color(getDisplay(),30,204,148));
		gc.drawLine(0, 4, e.x, 4);
		gc.dispose();
		isDrag = true;	
	}

	@Override
	public void mouseUp(MouseEvent e) {
		isDrag = false;
		progress = currentMouseX * 1.0 / getBounds().width;
		if (progress < 0)
			progress = 0;
		if (progress > 1)
			progress = 1;
		redraw();
	}

	@Override
	public void mouseMove(MouseEvent e) {
		if (isDrag) {
			progressPoint.setLocation(currentMouseX, 0);
			GC gc = new GC(this);
			gc.setLineWidth(5);
			gc.setForeground(new Color(getDisplay(),240, 240, 240 ));
			gc.drawLine(0, 4, getBounds().width, 4);
			gc.setForeground(new Color(getDisplay(),30,204,148));
			gc.drawLine(0, 4, currentMouseX, 4);
			gc.dispose();
			currentMouseX = e.x;
		}
	}
	
	@Override
	public void paintControl(PaintEvent e) {
		if(!isDrag) {
			progressPoint.setLocation((int)(progress * getBounds().width), 0);
			e.gc.setLineWidth(5);
			e.gc.setForeground(new Color(getDisplay(),240, 240, 240 ));
			e.gc.drawLine(0, 4, getBounds().width, 4);
			e.gc.setForeground(new Color(getDisplay(),30,204,148));
			e.gc.drawLine(0, 4, (int)(progress * getBounds().width), 4);
		}
	}

}
